//入口文件

//导入Vue
import Vue from 'vue'

//注册vuex
import Vuex from 'vuex'
Vue.use(Vuex)
//先获取本地存储的car
var car = JSON.parse(localStorage.getItem('car')||'[]')

var store = new Vuex.Store({
    state:{ //this.$store.state.***
      car:car
    },
    mutations:{ //this.$store.commit('方法名','按需传递的唯一参数')
    addToCar(state,goodsinfo){
        var flag=false

        state.car.some(
            item=>{
                if(item.id == goodsinfo.id){
                    item.count+=parseInt(goodsinfo.count)
                    flag = true
                    return true
                }
            })

            if(!flag){
                state.car.push(goodsinfo)
            }

            localStorage.setItem('car',JSON.stringify(state.car))

    },
    updateGoodsInfo(state,goodsinfo){
        state.car.some(item=>{
            if(item.id==goodsinfo.id){
                item.count = parseInt(goodsinfo.count)
                return true
            }
        })
        localStorage.setItem('car',JSON.stringify(state.car))
    },
    removeFromCar(state,id){
        state.car.some((item,i)=>{
            if(item.id ==id){
                state.car.splice(i,1)
                return true;
            }
        })
        localStorage.setItem('car',JSON.stringify(state.car))
        },
    updateGoodsSelected(state,info){
         state.car.some(item =>{
             if(item.id==info.id){
                 item.selected = info.selected
             }
         })
         localStorage.setItem('car',JSON.stringify(state.car))
    }
    },
    getters:{  //this.$store.getters.***
        getAllCount(state){
            var c=0;
            state.car.forEach(item=>{
                c+=item.count
            })
            return c
        },

        getGoodsCount(state){
            var o = {}
            state.car.forEach(item=>{
                o[item.id] = item.count 
            })
            return o
        },

        getGoodsSelected(state){
            var o = {}
            state.car.forEach(item=>{
                o[item.id] = item.selected
            })
            return o
        },
        getGoodsCountAndAmount(state){
            var o ={
                count:0,
                amount:0
            }
            state.car.forEach(item=>{
                if(item.selected){
                    o.count+=item.count
                    o.amount+=item.price*item.count
                }
            })
            return o
        }

    }

})

//导入补充样式
//import './lib/CSS/golbal.css'

//导入路由
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import router from './router.js'

//导入vue-resource
import VueResource from 'vue-resource'
Vue.use(VueResource)
Vue.http.options.root='http://www.liulongbin.top:3005';
Vue.http.options.emulateJSON = true;


//按需导入mintUI组件
// import { Header, Swipe, SwipeItem,Button,Lazyload } from 'mint-ui';
// Vue.component(Header.name, Header)
// Vue.component(Swipe.name, Swipe)
// Vue.component(SwipeItem.name, SwipeItem)
// Vue.component(Button.name,Button)
// Vue.use(Lazyload)


//全部导入mintUI组件
import MintUI from 'mint-ui'
Vue.use(MintUI)
import 'mint-ui/lib/style.css'

//导入MUI的样式
import './lib/MUI/css/mui.min.css'
import './lib/MUI/css/icons-extra.css'

//导入格式化时间的插件
import moment from 'moment'

//定义全局的过滤器
Vue.filter('dateFormat',function(dataStr,pattern ="YYYY-MM-DD HH:mm:ss"){
    return moment(dataStr).format(pattern)
})

//安装 图片预览插件
import VuePreview from 'vue-preview'
Vue.use(VuePreview)

//导入app.vue
import app from './App.vue'

var vm = new Vue({
    el:'#app',
    render:c=>c(app),
    router,
    store
})